unit TrackUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ImgList, IdUDPClient, Menus;

type
  TTrackStatusThread = class(TThread)
  private
    FSyncNodes: TStrings;
    FIp: string;
    FUdp: TIdUDPClient;
  protected
    procedure Execute; override;
  public
    constructor Create(AIp: string; AViewNodes: TStrings);
    destructor Destroy; override;
  end;

  TfrmTrack = class(TForm)
    Panel1: TPanel;
    lvTrack: TListView;
    ImageList1: TImageList;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    FSyncNodes: TStrings;
    FTrackThread: TTrackStatusThread;
    procedure UpdateTrack;
  public
    { Public declarations }
  end;

var
  frmTrack: TfrmTrack;

implementation

uses ProtocolUnit, MainUnit, FunctionUnit, ConstUnit;

{$R *.dfm}

{ TTrackStatusThread }

constructor TTrackStatusThread.Create(AIp: string; AViewNodes: TStrings);
begin
  FIp := AIp;
  FSyncNodes := AViewNodes;
  FUdp := TIdUDPClient.Create(nil);
  FreeOnTerminate := True;
  inherited Create(False);
end;

destructor TTrackStatusThread.Destroy;
begin

  inherited;
end;

procedure TTrackStatusThread.Execute;
var
  s: string;
begin
  inherited;
  while not Terminated do
  begin
    FUdp.Send(FIp, TrackUdpPort, IntToStr(Track_Status));
    s := FUdp.ReceiveString(2000);
    FSyncNodes.CommaText := s;

    Sleep(10000);
  end;
end;

procedure TfrmTrack.FormCreate(Sender: TObject);
begin
  FSyncNodes := TStringList.Create;
  FTrackThread := TTrackStatusThread.Create(GetSysParams(CommServerIp, Database), FSyncNodes);

  UpdateTrack;
end;

procedure TfrmTrack.FormDestroy(Sender: TObject);
begin
  FTrackThread.Terminate;
end;

procedure TfrmTrack.UpdateTrack;
var
  i: Integer;
  SL: TStrings;
begin
  if not Assigned(FSyncNodes) then
    Exit;

  lvTrack.Clear;
  SL := TStringList.Create;
  try
    for i := 0 to FSyncNodes.Count - 1 do
    begin
      FunctionUnit.ParseLine(FSyncNodes[i], '|', SL);
      if SL.Count < 3 then
        Exit;

      with lvTrack.Items.Add do
      begin
        Caption := SL[0];
        SubItems.Add(SL[1]);
        SubItems.Add(SL[2]);
      end;
    end;
  finally
    SL.Free;
  end;
end;

procedure TfrmTrack.N1Click(Sender: TObject);
begin
  UpdateTrack;
end;

procedure TfrmTrack.Timer1Timer(Sender: TObject);
begin
  UpdateTrack;
end;

end.
